package com.hna.face.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.hna.face.mapper.BfOrgMapper;
import com.hna.face.model.BfOrg;
import com.hna.face.model.TreeNode;
import com.hna.face.service.IBfOrgService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 组织信息表  服务实现类
 * </p>
 *
 * @author wx
 * @since 2017-06-25
 */
@Service
public class BfOrgServiceImpl extends ServiceImpl<BfOrgMapper, BfOrg> implements IBfOrgService {

	@Autowired
	private BfOrgMapper orgMapper;
	
	//查询组织机构及子机构
	@Override
	public List<Long> selectByPid(Long id) {
		// TODO Auto-generated method stub
		List<BfOrg> resultList=new  ArrayList<BfOrg>();
		List<BfOrg> orgList = orgMapper.selectByPid(id);
		if(orgList == null)
		{
			return null;
		}
		
		resultList.addAll(orgList);

		List<BfOrg> orgchildList=genChild(orgList);
		resultList.addAll(orgchildList);
		
		List<Long> orgIdList= new ArrayList<Long>();
		if(resultList.size()<1){
			return null;
		}
		for(int i=0;i<resultList.size();i++){
			orgIdList.add(resultList.get(i).getId());
		}

		
		return orgIdList;
	}
	
	public List<BfOrg> genChild(List<BfOrg> orgList) {
		if(orgList==null){
			return null;
		}
		List<BfOrg> resultList= new ArrayList<BfOrg>();
		for(int i=0;i<orgList.size();i++){
			
			List<BfOrg> org1List = orgMapper.selectByPid(orgList.get(i).getId());
			if(org1List == null)
			{
				continue;
			}

			resultList.addAll(org1List);
			List<BfOrg> orgchildList=genChild(org1List);
			resultList.addAll(orgchildList);		
		}		
		 return resultList;
	}
	

	@Override
	public List<TreeNode> getOrgTreeByPidOrg(TreeNode treeNode) {
		List<TreeNode> treeNodeList;
		if(treeNode!= null){
			 Wrapper<BfOrg> wrapper = new EntityWrapper<BfOrg>();
			 wrapper.eq("pid", treeNode.getId());
			 List<BfOrg> orgList = this.selectList(wrapper);
			 treeNodeList = new ArrayList<TreeNode>();
			 for(BfOrg BfOrg : orgList){
				 TreeNode tree = new TreeNode();
			     tree.setId(BfOrg.getId()+"");
			     tree.setParentId(BfOrg.getPid()+"");
		         tree.setText(BfOrg.getName());
		         tree.setChildren(getOrgTreeByPidOrg(tree));
		         treeNodeList.add(tree);
			 }
			 return treeNodeList;
		}
		
		return null;
	}
	
	@Override
	public List<BfOrg> getOrgListByPid(Long id) {
		
		List<BfOrg> resultList=new  ArrayList<BfOrg>();
		List<BfOrg> orgList = orgMapper.selectByPid(id);
		if(orgList == null)
		{
			return null;
		}
		
		resultList.addAll(orgList);

		List<BfOrg> orgchildList = genChild(orgList);
		resultList.addAll(orgchildList);
		
		if(resultList.size()<1){
			return null;
		}
		
		return resultList;
	}
	
	@Override
	public Long getGrandfatherOrgId(Long id,Integer num) {
		
		BfOrg resultList;
		Long pid = id;
		for(int i=0;i<num;i++){
			if(null!= pid){
				resultList = this.selectById(pid);
				pid = resultList.getPid();
			}
		}
		return pid;
	}
}
